public class Example
{

    public Example()
    {

    }

    /// 递归计算1+...100的值
    public static int Sum(int n)
    {
        if (n == 1)
        {
            return n;
        }
        return n + Sum(n - 1);
    }

    // 统计字符出现的次数
    public static void CountChar(string s)
    {
        Dictionary<char, int> dic = new Dictionary<char, int>();
        foreach (char c in s)
        {
            if (dic.ContainsKey(c))
            {
                dic[c]++;
            }
            else
            {
                dic.Add(c, 1);
            }
        }
        foreach (KeyValuePair<char, int> p in dic)
        {
            Console.WriteLine("字符{0},出现的次数{1}", p.Key.ToString(), p.Value.ToString());
        }
    }

    // 字符串转整数
    public static int StringToInt(string s)
    {
        int result = 0;
        if (string.IsNullOrWhiteSpace(s))
        { 
            return result;
        }
        bool minus = s[0] == '-' ? true : false;
        if (minus && s.Length == 1)
        {
            return result;
        }

        for (int i = minus ? 1 : 0; i < s.Length; i++)
        {
            if (s[i] >= '0' && s[i] <= '9')
            {
                result = s[i] - 48 + result * 10;
            }
            else
            {
                return result;
            }
        }
        return result;
    }

    // 判断奇偶数总数和
    public static int F2(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
        {
            if (i % 2 > 0)
            {
                sum += i;
            }
            else
            {
                sum -= i;
            }
        }
        return sum;
    }
}